package JUC.cas;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author mubei
 * @date 2021/11/23
 * @description
 */
public class AtmicTest {

    public static void main(String[] args) {
        //Integer   int
        AtomicStampedReference<Integer> atomic = new AtomicStampedReference<>(1, 1);
        new Thread(()->{
            int stamp = atomic.getStamp();
            System.out.println("a=>"+stamp);
            //  期待值  修改值   之前版本号   更新版本号
            System.out.println(atomic.compareAndSet(1, 2, atomic.getStamp(), atomic.getStamp()+1 ));
            System.out.println("a2=>"+atomic.getStamp());
            System.out.println(atomic.compareAndSet(2, 1, atomic.getStamp(), atomic.getStamp() + 1));
            System.out.println("a3=>"+atomic.getStamp());

        },"a").start();
        new Thread(()->{
            int stamp = atomic.getStamp();
            System.out.println("b=>"+stamp);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(atomic.compareAndSet(1, 44, stamp, stamp + 1));
            System.out.println("b1->"+atomic.getStamp());
        },"b").start();
    }
}
